package com.ybt.heartratem.common.app;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.ybt.heartratem.moudle.main.SplActivity;

import me.goldze.mvvmhabit.utils.KLog;

public class BootBroadcastReceiver extends BroadcastReceiver {
    public final static String ACTION = "android.intent.action.BOOT_COMPLETED";

    @Override
    public void onReceive(Context context, Intent intent) {
        /**
         * 接收更新广播
         * android.intent.action.PACKAGE_REPLACED
         * */
        if (intent.getAction().equals(Intent.ACTION_PACKAGE_REPLACED) || intent.getAction().equals(Intent.ACTION_PACKAGE_ADDED)) {
            String packageName = intent.getDataString().substring(8);
            KLog.e("BootReceiver onReceive():接收到Intent.getAction() = " + intent.getAction() + " , 包名 = " + intent.getDataString());
            KLog.e("BootReceiver onReceive():更新了:" + packageName + "包名的程序，context.getPackageName()=" + context.getPackageName());
            //更新的软件包名是否和我的工程一致
            if (packageName.equals("com.ybt.aifencan")) {
                //更新完后打开
                Intent mainActivityIntent = new Intent(context, SplActivity.class);
                mainActivityIntent.setAction("android.intent.action.MAIN");
                mainActivityIntent.addCategory("android.intent.category.LAUNCHER");
                mainActivityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(mainActivityIntent);
            }
        } else if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
            //开机广播
            Intent mainActivityIntent = new Intent(context, SplActivity.class);
            mainActivityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(mainActivityIntent);
        }
    }
}